var apiUrl = $("#apiUrl").val();
var basePath = $("#basePath").val();
var installId = location.search.split("=")[1];
$(document).ready(function () {
    menuCss("安装");
    getInstallInfo(installId);
    getEventList(installId);
    getFileList(installId);
})

function getInstallInfo(id){
	if(id){
		publicAjax(apiUrl+"install/installInfo","POST",{installId: id},function (data) {
			if(data.ret == 200){
				$("#hospitalContacts").text(data.data.hospitalContacts || '');
				$("#hospitalPhone").text(data.data.hospitalPhone || '');
				$("#projectChargePerson").text(data.data.projectChargePerson || '');
				$("#projectChargePersonPhone").text(data.data.projectChargePersonPhone || '');
				$("#serviceId").text(data.data.serviceName || '');
				$("#estimateInstallDate").text(data.data.estimateInstallDate || '');
				$("#actualInstallDate").text(data.data.actualInstallDate || '');
				$("#status").text(data.data.statusItem || '');
				$("#remark").text(data.data.installRemark || '');
				$("#deviceName").text(data.data.deviceName || '');
				$("#deviceModel").text(data.data.modelName || '');
				$("#placeOrigin").text(data.data.placeOrigin || '');
				$("#specifications").text(data.data.specifications || '');
				$("#price").text(data.data.contractDevicePrice || '');
				$("#attachment").text(data.data.contractDeviceAttachment || '');
				$("#quantity").text(data.data.purchaseQuantity || '');
				$("#upkeepCycle").text(data.data.upkeepCycle || '');
				$("#hospital").text(data.data.hospitalName || '');
				$("#deviceMan").text(data.data.deviceMaintainerName || '');
				$("#contractNo").text(data.data.contractNo || '');
				$("#manufacturerName").text(data.data.manufacturerName || '');
				$("#deviceWarrantyPeriod").text(data.data.deviceWarrantyPeriod || '');
				$("#supplierWarrantyPeriod").text(data.data.supplierWarrantyPeriod || '');
				$("#scoreTable").html('<tr><td class="text-center" colspan="2">暂无评分信息</td></tr>');
			    if(data.data.commentView && data.data.commentView.commentInfoList.length){
			    	$("#scoreTable").html("");
			    	$.each(data.data.commentView.commentInfoList,function (index,item) {
			    		$("#scoreTable").append("<tr>\
			    			<td>"+item.dividTypeName+"</td>\
			    			<td>"+item.value+"分（"+scoreText(item.value)+"）</td>\
			    		</tr>");
			    	})
			    	$("#scoreTable").append("<tr>\
		    			<td>评论</td>\
		    			<td>"+data.data.commentView.commentContent+"</td>\
		    		</tr>");
			    }
			}else{
				flavrShowByTime("获取信息失败",null,"danger",false);
			}
		})
	}
}
//获取重大事件
function getEventList(id){
	if(id){
		publicAjax(apiUrl+"event/eventList","POST",{keyType: 1600, keyId: id},function (data) {
			if(data.ret == 200){
				var str = '';
				if(data.data && data.data.length){
					$.each(data.data,function (index,item) {
			            var classs="fa fa-clock-o";
			            var color="#5082eb";
			            str+="<li><i class='"+classs+"' style='color: #fff;background:"+color+"'></i>"
			                +"<div class='timeline-item'>"
			                +"<h3 class='timeline-header' style='background: #fff'>"
			                +"<span style='color: #000'>"+(item.eventDateTime==null?'':item.eventDateTime)+"</span>"
			                +"</h3>"
			                +"<div class='timeline-body'>"
			                +"<span style='color: #5082eb'>"+(item.eventTitle==null?'': '【'+item.eventTitle+'】 ')+"</span>"
			                +item.eventContent
			                +"</div>"
			                +"<div>";

			            if(item.fileList != null && item.fileList.length > 0){
			            	var imgType = ["jpg", "png", "jpeg", "gif","JPG","JPEG", "PNG", "GIF"];
			                $.each(item.fileList,function (index,items) {
			                    if(imgType.indexOf(items.fileSuffix) > -1){
			                		str+= "<img class='imgList' style = 'width:100px;height:30px;' src='"+items.fileUrl+"' alt='Visa'> &nbsp;";
			                	}else{
			                		str+= "<p style='padding-left: 10px'><a target='_blank' style='cursor: pointer' href='"+items.fileUrl+"'>"+items.fileName+"</a></p>"
			                	}
			                })
			            }
			            str+="</div></div></li>";
			        });
			        str+="<li><i class='fa fa-clock-o bg-gray'></i></li>";
			        $("#timeAxis").html(str);
					$(".imgList").viewer("destroy");
			        $(".imgList").viewer();
				}else{
					str = "暂无事件";
	        		$("#timeline").html(str);
				}
			}
		})
	}
}

//获取文件列表
function getFileList(id){
	if(id){
		publicAjax(apiUrl+"file/queryFileList","POST",{keyType: 1404, keyId: id},function (data) {
			$("#installFileTable").html("");
			if(data.ret == 200){
				var strs = "";
				if(data.data.length){
					$.each(data.data,function (index,item) {
		            	var fileName = item.fileName + "." + item.fileSuffix;
			            strs += "<tr><td class='text-muted text-center'>"+(index+1)+"</td>" +
			                "<td class='text-muted text-center'>"+(item.fileTypeName == null?'':item.fileTypeName)+"</td>" +
			                "<td class='text-muted text-center'><a href='javascript:showFile(\""+item.fileUrl+"\",\""+fileName+"\")'>"+(item.fileName == null?'':item.fileName)+"</a></td>" +
                            "<td class='text-muted text-center'><button type='button' class='btn btn-danger btn-xs' title='下载' onclick='fileDownload(\""+item.fileName+"\",\""+item.fileUrl+"\",\""+item.fileSuffix+"\")'><i class='fa fa-fw fa-download'></i></button></td>" +
			                "</tr>";
			        });
				}else{
			        strs = "<tr><td colspan='10' class='text-muted text-center'>暂无文件</td></tr>";
			    }
		    	$("#installFileTable").html(strs);
			}
		})
	}
}

function showFile(url,file){
    $("#file_show_modal").iziModal({
        zindex:999999,
        headerColor: '#00c0ef',
        title: '预览',
        subtitle: file,
        overlayClose: true,
        history:false,
        iframe : true,
        iframeURL:url,
        fullscreen: true,
        openFullscreen: false,
        onClosed:function(){
            $('#file_show_modal').iziModal('destroy');
        }
    });
    $("#file_show_modal").iziModal('open');
}

function fileDownload(fileName,filePath,fileSuffix){
    location.href= basePath+"/file/fileDownload?filePath="+filePath+"&fileName="+fileName;
}